package com.lcxw.hxzbapi.controller.unioncontroller;

import ch.qos.logback.classic.jmx.MBeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lcxw.hxzbapi.entity.pojo.IncomeUnion;
import com.lcxw.hxzbapi.entity.pojo.Union;
import com.lcxw.hxzbapi.entity.requestBean.GetUnionIncomeReq;
import com.lcxw.hxzbapi.entity.requestBean.GetUnionInfoReq;
import com.lcxw.hxzbapi.entity.requestBean.RegistUnionReq;
import com.lcxw.hxzbapi.entity.requestBean.ResetPasswordReq;
import com.lcxw.hxzbapi.entity.requestBean.ResetUnionInfoReq;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.entity.response.ResponseData;
import com.lcxw.hxzbapi.entity.response.UnionIncomeResp;
import com.lcxw.hxzbapi.service.IncomeUnionService;
import com.lcxw.hxzbapi.service.UnionService;
import com.lcxw.hxzbcommon.common.AesCBCUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
public class UnionRealization {

    @Resource
    UnionService unionService;

    @Resource
    IncomeUnionService incomeUnionService;

    public Response registUnion(RegistUnionReq registUnionReq) {
        String unionName = registUnionReq.getUnionName();
        String telephone = registUnionReq.getTelephone();
        if (unionService.selectByName(unionName) != null
            || unionService.selectByTelephone(telephone) != null) {
            return Response.error(ErrorCode.ALREADY_EXIST, "公会名已存在或手机号码已注册工会");
        }

        String password = registUnionReq.getPassword();
        try {
            password = AesCBCUtil.getInstance().encrypt(password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Union union = new Union();
        BeanUtils.copyProperties(registUnionReq, union);
        double proportion = registUnionReq.getProportion() / 100;
        union.setProportion(proportion);
        union.setPassword(password);
        union.setStatus("审核中");
        unionService.insert(union);
        return Response.ok();
    }

    //获取期次
    public Response getIncomeTerms() {
        List<String> strings = incomeUnionService.selectTerms();
        return Response.ok(strings);
    }

    //获取公会收入记录
    public Response getUnionIncome(GetUnionIncomeReq getUnionIncomeReq) {

        String telephone = getUnionIncomeReq.getUserId();
        int unionId = getUnionIncomeReq.getUnionId();
        Union union = unionService.selectById(unionId);
        if (union == null || !union.getTelephone().equals(telephone)) {
            return Response.error(ErrorCode.NO_PERMISSION, "你不是该工会会长或公会不存在，无法获取记录。");
        }

        String term = getUnionIncomeReq.getTerm();
        String status = getUnionIncomeReq.getStatus();
        int pageNum = getUnionIncomeReq.getPageNum();
        int pageSize = getUnionIncomeReq.getPageSize();

        IPage<IncomeUnion> page = incomeUnionService
            .selectList(unionId, term, status, pageNum, pageSize);
        long total = page.getTotal();
        List<IncomeUnion> records = page.getRecords();
        List<UnionIncomeResp> list = new ArrayList<>();
        for (IncomeUnion incomeUnion : records
        ) {
            UnionIncomeResp unionIncomeResp = new UnionIncomeResp();
            BeanUtils.copyProperties(incomeUnion, unionIncomeResp);
            unionIncomeResp.setUnionName(union.getUnionName());
            unionIncomeResp.setCompany(union.getCompany());
            unionIncomeResp.setPublicAccounts(union.getPublicAccounts());
            unionIncomeResp.setTaxNum(union.getTaxNum());

            list.add(unionIncomeResp);
        }
        ResponseData data = new ResponseData();
        data.setTotal(total);
        data.setList(list);
        return Response.ok(data);
    }

    public Response getUnionInfo(GetUnionInfoReq getUnionInfoReq) {

        int unionId = getUnionInfoReq.getUnionId();
        Union union = unionService.selectById(unionId);
        return Response.ok(union);

    }

    public Response resetUnionInfo(ResetUnionInfoReq resetUnionInfoReq) {

        int unionId = resetUnionInfoReq.getUnionId();
        Union union = unionService.selectById(unionId);

        String img = resetUnionInfoReq.getImg();
        String info = resetUnionInfoReq.getInfo();
        String invite = resetUnionInfoReq.getInvite();
        double proportion = resetUnionInfoReq.getProportion();
        proportion = proportion / 100;

        union.setImg(img);
        union.setInfo(info);
        union.setInvite(invite);
        union.setProportion(proportion);
        unionService.update(union);
        return Response.ok();

    }

    public Response resetPassword(ResetPasswordReq resetPasswordReq) throws Exception {

        int unionId = resetPasswordReq.getUnionId();

        Union union = unionService.selectById(unionId);
        String oldPassword = resetPasswordReq.getOldPassword();
        AesCBCUtil instance = AesCBCUtil.getInstance();
        String encrypt = instance.encrypt(oldPassword);

        if (!union.getPassword().equals(encrypt)) {
            return Response.error(ErrorCode.INVALID_PARAM, "旧密码错误，请确认。");
        }
        String password = resetPasswordReq.getPassword();
        password = instance.encrypt(password);
        union.setPassword(password);
        unionService.update(union);
        return Response.ok();
    }
}
